<?php
/*后台父级控制器*/
namespace app\common\controller;

use think\Controller;
use think\Db;
use think\Session;

class AdminBase extends Controller
{
    protected function _initialize()
    {
        parent::_initialize();

        $this->checkAuth();
        $this->getNavHtml(); //输出HTML导航
        //$this->getMenuDefault(); //默认主题菜单
        $this->getGroup();
    }
    /**
     * 输出权限组
     */
    protected function getGroup(){
        $group_info = Db::name('auth_group')->column('title', 'id');
        $this->assign('group_info',$group_info);
    }
    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth()
    {

        if (!Session::has('admin_id')) {
            $this->redirect('Hslogin/index');
        }

        $controller = $this->request->controller();
        $action     = $this->request->action();

        $group_id = Session::get('group_id');   //超级管理组所有成员有所有权限，跳过验证
            
        if ($group_id != 1 && !$this->check($controller . '/' . $action, $group_id)) {
            $this->error('没有权限');
        }
    }

    /**
     * 取得后台菜单的Html形式
     *
     * @param string $permission
     * @return
     */
    protected final function getNavHtml() {
        $_menu = $this->getMenu();
        $top_nav = '';
        $left_nav = '';
        foreach ($_menu as $key => $value) {
            $top_nav .= '<li data-param="' . $key . '"><a href="javascript:void(0);">' . $value['title'] . '</a></li>';
            $left_nav .= '<div id="admincpNavTabs_'. $key .'" class="nav-tabs">';
            if (!empty($value['child'])) {
                foreach ($value['child'] as $ke => $val) {
                    if (!empty($val['child'])) {
                        $icon = $val['icon']?$val['icon']:'fa fa-sitemap';
                        $left_nav .= '<dl><dt><a href="javascript:void(0);"><span class="' . $icon .'"></span><h3>' . $val['title'] . '</h3></a></dt>';
                        $left_nav .= '<dd class="sub-menu"><ul>';
                        foreach ($val['child'] as $k => $v) {
                            $left_nav .= '<li><a href="javascript:void(0);" data-param="' . $key . '|' . $k .'|' .url($v['name']) .'">' . $v['title'] . '</a></li>';
                        }

                        $left_nav .= '</ul></dd></dl>';
                    }
                }
            }
            $left_nav .= '</div>';
        }
        $this->assign('top_nav',$top_nav);
        $this->assign('left_nav',$left_nav);
    }

    /**
     * 获取侧边栏菜单
     */
    protected function getMenu()
    {
        $menu     = [];
        $group_id = Session::get('group_id');

        $auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(['sort' => 'DESC', 'id' => 'ASC'])->select();

        foreach ($auth_rule_list as $value) {
            if ($group_id == 1 || $this->check($value['name'], $group_id)) {
                $menu[] = $value;
            }
        }
        $menu = !empty($menu) ? list_to_tree($menu,'id','pid','child') : [];
        return $menu;
    }

    /**
     * 获取侧边栏菜单
     */
    protected function getMenuDefault()
    {
        $admin_menu     = [];
        $group_id = Session::get('group_id');

        $auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(['sort' => 'DESC', 'id' => 'ASC'])->select();

        foreach ($auth_rule_list as $value) {
            if ($group_id == 1 || $this->check($value['name'], $group_id)) {
                $admin_menu[] = $value;
            }
        }
        $admin_menu = !empty($admin_menu) ? array2tree($admin_menu) : [];
        $this->assign('menu', $admin_menu);
    }

    /*当前操作验证权限*/
    protected function check($name,$group_id){
        $rules = db('auth_group')->field('rules')->find($group_id);
        //当前权限组授权的操作值
        $authList = db('auth_rule')->where(['id'=>['in',$rules['rules']]])->column('name');
        //所有的操作值
        $auth_rule = db('auth_rule')->column('name');//column('name')仅包含name字段的一维数组
        //仅当前操作存在auth_rule表中，但不存在授权的列表中authList时无权限
        if(in_array_case($name,$auth_rule) && !in_array_case($name,$authList)){ //in_array_case不区分大小写的in_array
            return false;
        }
        return true;
    }
}